//
//  ConsulationV2AskViewModel.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/11/1.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "ConsulationV2AskViewModel.h"

@implementation ConsulationV2AskViewModel

#pragma mark - Ovrride
- (void)initialize
{
    [super initialize];
    
    @weakify(self)
    
    self.uploadImgs = [NSMutableArray array];
    
    self.addImgCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        
        @strongify(self);
        
        NSMutableArray* newUploadImgs = [NSMutableArray arrayWithArray:[self.uploadImgs arrayByAddingObjectsFromArray:input]];
        
        self.uploadImgs = newUploadImgs;
        
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            
            [subscriber sendNext:nil];
            [subscriber sendCompleted];
            
            return nil;
        }];
    }];
    
    self.deleteImgCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        
        @strongify(self);
        
        self.uploadImgs = [NSMutableArray arrayWithArray:self.uploadImgs];
        
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            
            [subscriber sendNext:nil];
            [subscriber sendCompleted];
            
            return nil;
        }];
    }];
    
    self.submitCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        
        @strongify(self);
        
        @weakify(self);
        return [[self checkSubmitSignal] then:^RACSignal *
                {
                    @strongify(self);
                    
                    return [self requestSubmitSignal];
                }];
    }];
}

- (RACSignal*)checkSubmitSignal
{
    NSString* result = @"";
    if (![result isNotBlank] && ![self.askTitle isNotBlank])
    {
        result = @"请填写标题";
    }
    
    if (![result isNotBlank] && ![self.content isNotBlank])
    {
        result = @"请填写咨询内容";
    }
    
    if (![result isNotBlank] && ![self.giveGold isNotBlank])
    {
        result = @"请填写悬赏值";
    }
    
    NSInteger giveGold = [self.giveGold integerValue];
    
    if (![result isNotBlank] && giveGold<15)
    {
        result = @"悬赏值不能低于15生态币";
    }
    
    UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
    NSInteger myGold = [um.userData.money integerValue];
    
    if (![result isNotBlank] && myGold < giveGold)
    {
        result = @"生态币不足";
    }
    
    if ([result isNotBlank])
    {
        return [RACSignal error:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:result}]];
    }
    else
    {
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
                {
                    [subscriber sendNext:nil];
                    [subscriber sendCompleted];
                    
                    return nil;
                }];
    }
}

- (RACSignal*)requestSubmitSignal
{
    @weakify(self);
    
    UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
            {
                @strongify(self);
                
//                NSMutableArray* afuploadImgs = [NSMutableArray array];
//                for (NSInteger index=0; index<self.uploadImgs.count; index++)
//                {
//                    UIImage* curImg = self.uploadImgs[index];
//
//                    AFUploadImage* afUploadImg = [AFUploadImage createWithImage:curImg info:@{@"Name":@"Images"}];
//                    [afuploadImgs addObject:afUploadImg];
//                }
                
                //咨询类型1公共提问2专对专家
                NSString* type = (self.expertID && [self.expertID isNotBlank]) ? @"2" : @"1";
                NSString* expertID = HCStrValid(self.expertID);
                
                NSString* uploadImg = @"";
                if (self.uploadImgs.count > 0)
                {
                    UIImage* currentImg = self.uploadImgs[0];
                    CGSize uImgSize = currentImg.size;
                    UIImage* newUploadImg = [currentImg hyb_clipToSize:(CGSize){100, 100*uImgSize.height/uImgSize.width} isEqualScale:YES];
                    uploadImg = [UIImagePNGRepresentation(newUploadImg) base64EncodedStringWithOptions:0];
                }
                
                @weakify(self);
                [[AFNetWorkUtils racPOSTWthURL:url_interface_consultsubmit params:interface_param_consultsubmit(um.userData.userID, type, expertID, self.askTitle, self.content, self.giveGold, uploadImg)] subscribeNext:^(id x)
                 {
                     @strongify(self);
                     
                     NSDictionary* result = x;
                     
                     [subscriber sendNext:result[SUCommandSysMessageKey]];
                     [subscriber sendCompleted];
                 } error:^(NSError *error)
                 {
                     //错误处理
                     
                     [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:error.userInfo[customErrorInfoKey]}]];
                     [subscriber sendCompleted];
                 }];
                
                return nil;
            }];
}

@end
